<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/main.css" />
  <title>聊天机器人</title>
</head>

<body>
  <div class="wrap">
    <!-- 头部 Header 区域 -->
    <div class="header">
      <h3>小思同学</h3>
      <img src="img/person01.png" alt="icon" />
    </div>
    <!-- 中间 聊天内容区域 -->
    <div class="main">
      <ul class="talk_list" style="top: 0px;" id="talk_list">
        <!-- 机器人 -->
        <li class="left_word">
          <img src="img/person01.png" /> <span>嗨，最近想我没有？</span>
        </li>
        <!-- 我 -->
        <!-- <li class="right_word">
          <img src="img/person02.png" /> <span>嗨，最近想我没有？</span>
        </li> -->
      </ul>
    </div>
    <!-- 底部 消息编辑区域 -->
    <div class="footer">
      <img src="img/person02.png" alt="icon" />
      <input type="text" placeholder="说的什么吧..." class="input_txt" id="ipt" />
      <input type="button" value="发 送" class="input_sub" id="btnSend" />
    </div>
  </div>
  <script type="text/javascript" src="./js/axios.js"></script>
  <audio src="" id="voice" autoplay style="display: none;"></audio>
  <script>
    let ipt = document.querySelector('#ipt')
    let btnSend = document.querySelector('#btnSend')
    let ul = document.querySelector('#talk_list')
    let voice = document.querySelector('#voice')

    // 发布功能
    btnSend.addEventListener('click', releaseWord)
    ipt.addEventListener('keyup', function (e) {
      if (e.key === 'Enter') {
        releaseWord()
      }
    })
    //封装发布功能函数
    function releaseWord() {
      const inputText = ipt.value
      if (inputText.trim()) {
        let myWord = document.createElement('li')
        myWord.classList.add('right_word')
        myWord.innerHTML = `<img src="img/person02.png" /> <span>${inputText}</span>`
        ul.appendChild(myWord)
        ipt.value = ''
        ul.scrollTop = ul.scrollHeight
        axios({
          method: 'get',
          url: 'http://ajax-api.itheima.net/api/robot',
          params: {
            spoken: inputText
          }
        }).then(({ data: res }) => {
          // console.log(res)
          let robotWord = document.createElement('li')
          robotWord.classList.add('left_word')
          robotWord.innerHTML = `<img src="img/person01.png" /> <span>${res.data.info.text}</span>`
          ul.appendChild(robotWord)
          ul.scrollTop = ul.scrollHeight
          getVoice(res.data.info.text)
        })
      }

      // console.log(inputText)
    }
    // 封装 ajax请求，将机器人的文字转语音 
    function getVoice(text) {
      axios({
        url: 'http://www.liulongbin.top:3006/api/synthesize',
        method: 'GET',
        params: {
          text
        }
      }).then(({ data: res }) => {
        // console.log(res)
        // 将机器人的文字转语音的结果（voiceUrl）赋值给audio的src属性
        voice.src = res.voiceUrl
      })
    }

  </script>
</body>

</html>